package com.example.mttab;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;


import java.util.List;

/**
 * 萌萌底部导航
 */
public class MMBottomNavView extends FrameLayout {
    private LinearLayout container;

    private OnTabSelectListener onTabSelectListener;

    public OnTabSelectListener getOnTabSelectListener() {
        return onTabSelectListener;
    }

    public void setOnTabSelectListener(OnTabSelectListener onTabSelectListener) {
        this.onTabSelectListener = onTabSelectListener;
    }

    public MMBottomNavView(Context context) {
        this(context,null);
    }

    public MMBottomNavView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MMBottomNavView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View.inflate(context,R.layout.view_mmbottom_nav,this);
        container = (LinearLayout) findViewById(R.id.container);

    }

    public void setNavData(List<MMBottomNavDataBean> navData,int defaultPos){
        container.removeAllViews();
        for (int i = 0; i < navData.size(); i++) {
            MMBottomNavItemView mmBottomNavItemView = new MMBottomNavItemView(getContext());
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
            layoutParams.weight = 1;
            mmBottomNavItemView.setLayoutParams(layoutParams);
            mmBottomNavItemView.setData(navData.get(i));
            mmBottomNavItemView.selectItem(defaultPos == i);
            int finalI = i;
            mmBottomNavItemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(onTabSelectListener != null){
                        onTabSelectListener.onSelect(finalI);
                    }
                    selectItem(finalI);
                }
            });
            container.addView(mmBottomNavItemView);
        }
    }

    /**
     * 设置消息数量
     * @param pos
     * @param msgNum
     */
    public void setMsgNum(int pos,int msgNum){
        if(container != null && container.getChildCount() > pos){
            MMBottomNavItemView mmBottomNavItemView = (MMBottomNavItemView) container.getChildAt(pos);
            mmBottomNavItemView.setMsgNum(msgNum);
        }
    }

    public void selectItem(int pos){
        for (int i = 0; i < container.getChildCount(); i++) {
            MMBottomNavItemView mmBottomNavItemView = (MMBottomNavItemView) container.getChildAt(i);
            mmBottomNavItemView.selectItem(i == pos);
        }
    }

    public interface OnTabSelectListener{
        void onSelect(int pos);
    }

    public static class MMBottomNavDataBean{
        private String name;
        private int selectTextColor;
        private int defaultTextColor;
        private int selectImageRes;
        private int defaultImageRes;

        public MMBottomNavDataBean(String name, int selectTextColor, int defaultTextColor, int selectImageRes, int defaultImageRes) {
            this.name = name;
            this.selectTextColor = selectTextColor;
            this.defaultTextColor = defaultTextColor;
            this.selectImageRes = selectImageRes;
            this.defaultImageRes = defaultImageRes;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getSelectTextColor() {
            return selectTextColor;
        }

        public void setSelectTextColor(int selectTextColor) {
            this.selectTextColor = selectTextColor;
        }

        public int getDefaultTextColor() {
            return defaultTextColor;
        }

        public void setDefaultTextColor(int defaultTextColor) {
            this.defaultTextColor = defaultTextColor;
        }

        public int getSelectImageRes() {
            return selectImageRes;
        }

        public void setSelectImageRes(int selectImageRes) {
            this.selectImageRes = selectImageRes;
        }

        public int getDefaultImageRes() {
            return defaultImageRes;
        }

        public void setDefaultImageRes(int defaultImageRes) {
            this.defaultImageRes = defaultImageRes;
        }
    }

}
